const app = getApp()
const api = app.globalData.api
Page({
    data: {
        problemOption: [],
        item: {},
    },
    formSubmit: async function (e) {
        let data = e.detail.value,
            id = data.id ? data.id : api.suiji(),
            problemData = wx.getStorageSync('problemData') ? wx.getStorageSync('problemData') : {}
        if (id) {
            // 初始化部分值
            data['num'] = api.initNaN(data, 'num')
            data['id'] = id
            problemData[id] = data
            api.showToast('选择成功')
            wx.setStorageSync('problemData', problemData)
            wx.navigateBack({
                delta: 1
            })
        }
    },
    showPopup: function (obj) {
        let popup = obj.currentTarget.dataset.show
        if (popup) {
            this.setData({
                [popup]: true
            });
        }
    },
    onDisplay: function (e) {
        let obj = e.currentTarget.dataset
        let prefix = obj.prefix ? obj.prefix : 'item'
        this.setData({
            calendar: true,
            prefix: prefix,
        })
    },
    // 确认选择
    onConfirm: function (e) {
        let obj = e.currentTarget.dataset
        let prefix = obj.prefix ? obj.prefix : 'item'
        let field = (prefix ? prefix + '.' : '') + obj.field
        this.setData({
            [obj.target]: false,
            [field]: this.funRouting(obj.field, e.detail, prefix),
        })
    },
    // 关闭
    onClose: function (e) {
        let obj = e.currentTarget.dataset
        if (obj) {
            this.setData({[obj.target]: false})
        }
    },
    goto(e) {
        return api.goto(e)
    },
    funRouting: function (type, detail) {
        switch (type) {
            default:
                return detail.value
        }
    },
    onLoad: async function (obj) {
        console.log(obj)
        const option = obj.data ? api.jsonToArray(obj.data) : []
        const item = obj.now ? api.jsonToArray(obj.now) : {}
        this.setData({
            problemOption: option,
            item: item
        })
    }
});
